<script setup>
import { ref, reactive, onMounted, getCurrentInstance } from 'vue'
import utils from '@/utils/utils.js'

//   获取Composition API 上下文对象
const { ctx } = getCurrentInstance()
// 获取全局挂载的属性
const { $api, $message } =
  getCurrentInstance().appContext.config.globalProperties

const loading = ref(true)

// 初始化用户表单对象 reactive 一般创建引用类型
const menuList = ref([])

// const loading = ref(true)
// 表单列数据
const columns = reactive([
  {
    label: '菜单名称',
    prop: 'menuName',
    width: 150
  },
  {
    label: '图标',
    prop: 'icon'
  },
  {
    label: '菜单类型',
    prop: 'menuType',
    formatter(row, column, value) {
      return {
        1: '菜单',
        2: '按钮'
      }[value]
    }
  },
  {
    label: '权限标识',
    prop: 'menuCode'
  },
  {
    label: '路由地址',
    prop: 'path'
  },
  {
    label: '组件路径',
    prop: 'component'
  },
  {
    label: '菜单状态',
    prop: 'menuState',
    width: 90,
    formatter(row, column, value) {
      return {
        1: '正常',
        2: '停用'
      }[value]
    }
  },
  {
    label: '创建时间',
    prop: 'createTime',
    formatter(row, column, value) {
      return utils.formateDate(new Date(value))
    }
  }
])
// 菜单表单
const menuForm = reactive({
  parentId: [null],
  menuType: 1,
  menuState: 1
})
// dialog弹出框初始值
const outerVisible = ref(false)
const action = ref('add')

//重置
const handleReset = (form) => {
  ctx.$refs[form].resetFields()
}
// 表单数据对象
const queryForm = reactive({
  menuState: 1
})

// 表单定义规则
const rules = reactive({
  menuName: [
    {
      required: true,
      message: '请输入菜单名称',
      trigger: 'blur'
    },
    {
      min: 2,
      max: 10,
      message: '长度在2-8个字符',
      trigger: 'blur'
    }
  ]
})

onMounted(() => {
  getMenuList()
})

const getMenuList = async () => {
  try {
    let list = await $api.getMenuList(queryForm)
    menuList.value = list
    setTimeout(() => {
      loading.value = false
    }, 300)
    // console.log(list)
  } catch (error) {
    throw new Error(error)
  }
}

// 编辑功能
const handleEdit = (row) => {
  outerVisible.value = true
  action.value = 'edit'
  // console.log(action)
  ctx.$nextTick(() => {
    // 将row的数据赋值到userForm
    Object.assign(menuForm, row)
  })
}

// 删除菜单
const handleDel = async (_id) => {
  // console.log(_id)
  await $api.menuSubmit({ _id, action: 'delete' })
  $message.success('删除成功')
  getMenuList()
}

// 关闭
const handleClose = () => {
  handleReset('dialogForm')
  outerVisible.value = false
}

// 新增功能
const handleAdd = (type, row) => {
  outerVisible.value = true
  action.value = 'add'
  if (type == 2) {
    menuForm.parentId = [...row.parentId, row._id].filter((item) => item)
  }
}
// console.log(action.value)
// 提交新增用户数据
const handleSubmit = () => {
  // console.log(e)
  ctx.$refs.dialogForm.validate(async (valid) => {
    if (valid) {
      let parmas = { ...menuForm }
      parmas.action = action.value
      // console.log(parmas)
      await $api.menuSubmit(parmas)
      // console.log(res)
      outerVisible.value = false
      $message.success('操作成功')
      handleReset('dialogForm')
      getMenuList()
    }
  })
}
</script>
<template>
  <div class="userForm">
    <!-- 搜索视图 -->
    <el-card class="box-card">
      <el-form
        ref="form"
        :inline="true"
        class="demo-form-inline"
        :model="queryForm"
        :rules="rules"
      >
        <el-form-item label="菜单名称" prop="menuName">
          <el-input placeholder="请输入菜单名称" v-model="queryForm.menuName" />
        </el-form-item>
        <el-form-item label="菜单状态" prop="menuState">
          <el-select v-model="queryForm.state">
            <el-option label="正常" :value="1" />
            <el-option label="停用" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getMenuList" size="default"
            >查询</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button ize="default" @click="handleReset('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card">
      <el-form :inline="true" class="demo-form-inline action">
        <el-form-item>
          <el-button type="primary" @click="handleAdd(1)" size="default"
            >新增</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        :data="menuList"
        v-loading="loading"
        style="width: 100%"
        row-key="_id"
        :tree-props="{ children: 'children' }"
      >
        <el-table-column
          v-for="item in columns"
          :prop="item.prop"
          :key="item.prop"
          :label="item.label"
          :width="item.width"
          :formatter="item.formatter"
        ></el-table-column>
        <el-table-column label="操作" width="220">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              @click="handleAdd(2, scope.row)"
              >新增</el-button
            >
            <el-button size="small" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="small"
              @click="handleDel(scope.row._id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 弹出框 -->
    <el-dialog title="新增用户" v-model="outerVisible" width="50%" height="80%">
      <el-form
        label-width="120px"
        :model="menuForm"
        ref="dialogForm"
        :rules="rules"
      >
        <el-form-item label="用户名" prop="userName">
          <el-cascader
            v-model="menuForm.parentId"
            :options="menuList"
            :props="{ checkStrictly: true, value: '_id', label: 'menuName' }"
            clearable
          >
          </el-cascader>
          <span>不选择,则直接创建一级菜单</span>
        </el-form-item>
        <el-form-item label="菜单类型" prop="menuType">
          <el-radio-group v-model="menuForm.menuType">
            <el-radio :label="1">菜单</el-radio>
            <el-radio :label="2">按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="菜单名称" prop="menuName">
          <el-input placeholder="请输入菜单名称" v-model="menuForm.menuName" />
        </el-form-item>
        <el-form-item
          label="菜单图标"
          prop="icon"
          v-show="menuForm.menuType == 1"
        >
          <el-input v-model="menuForm.icon" placeholder="请输入菜单图标" />
        </el-form-item>
        <el-form-item
          label="路由地址"
          prop="path"
          v-show="menuForm.menuType == 1"
        >
          <el-input v-model="menuForm.path" placeholder="请输入路由地址" />
        </el-form-item>
        <el-form-item
          label="权限标识"
          prop="menuCode"
          v-show="menuForm.menuType == 2"
        >
          <el-input v-model="menuForm.menuCode" placeholder="请输入权限标识" />
        </el-form-item>
        <el-form-item
          label="组件路径"
          prop="component"
          v-show="menuForm.menuType == 1"
        >
          <el-input v-model="menuForm.component" placeholder="请输入组件路径" />
        </el-form-item>
        <el-form-item
          label="菜单状态"
          prop="component"
          v-show="menuForm.menuType == 1"
        >
          <el-radio-group v-model="menuForm.menuState">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="handleSubmit"> 确认 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<style lang="scss">
.box-card {
  margin-top: 25px;
}
.pagination {
  margin-top: 1.5rem;
  display: flex;
  justify-content: end;
}
</style>
